package com.srb.sms;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.srb.sms.util.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class SDKTestSendTemplateSMS {

    /*
    rly:
      accountSid: 8aaf070881ad8ad40181fa4b064512c1
      accountToken: bdad2a799b1e487b8aea3da4d26ce2cd
      appId: 8aaf070881ad8ad40181fa4b072812c8
      serverIp: app.cloopen.com
      serverPort: 8883
     */
    @Value("${rly.accountSid}")
    private String accountSid;
    @Value("${rly.accountToken}")
    private String accountToken;
    @Value("${rly.appId}")
    private String appId;

    @Value("${rly.serverIp}")
    private String serverIp;
    @Value("${rly.serverPort}")
    private String serverPort;

    public void send(String mobile, Map<String,Object> params){
        CCPRestSmsSDK ccpRestSmsSDK = new CCPRestSmsSDK();
        // 初始化主账号名称和主账号令牌，登陆云通讯网站后，
        // 可在控制首页中看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN。
        ccpRestSmsSDK.setAccount(accountSid,accountToken);
        // 请使用管理控制台中已创建应用的APPID。
        ccpRestSmsSDK.setAppId(appId);
        ccpRestSmsSDK.setBodyType(BodyType.Type_JSON);
        // 初始化服务器地址和端口，生产环境配置成app.cloopen.com，端口是8883.
        ccpRestSmsSDK.init(serverIp,serverPort);
        //验证码为生成的随机数，5分钟内到期
        System.out.println("params:"+params);
        String[] datas = {(String) params.get("code"),"5"};
        System.out.println("datas:"+datas);
        //restAPI.sendTemplateSMS("号码1,号码2等","模板Id" ,new String[]{"模板内容1","模板内容2"});
        HashMap<String, Object> result = ccpRestSmsSDK.sendTemplateSMS(mobile, "1", datas);
        System.out.println("result:"+result);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key:keySet){
                Object object = data.get(key);
                System.out.println(key+"="+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }
    }

    /*
    params:{code=1234}
    datas:[Ljava.lang.String;@41fc0084
    result:{data={templateSMS={dateCreated=20220714163144, smsMessageSid=42f6b04cedc649d2916c18c0a60b1916}}, statusCode=000000}
    templateSMS={dateCreated=20220714163144, smsMessageSid=42f6b04cedc649d2916c18c0a60b1916}
     */

    @Test
    public void testSend(){
        Map<String, Object> map = new HashMap<>();
        map.put("code","1234");
        send("18986133504",map);
    }

    @Test
    public void testProperties(){
        System.out.println(SmsProperties.ACCOUNT_SID);
        System.out.println(SmsProperties.APP_ID);
        System.out.println(SmsProperties.ACCOUNT_TOKEN);
        System.out.println(SmsProperties.SERVER_PORT);
        System.out.println(SmsProperties.SERVER_IP);
    }


}
